$(function () {
    getSpecialConfig();
    getLimitData();
    var channel = getQueryString("channel");
    $(".back-wrapper").click(function () {
        location.href = "../index.html?channel="+channel;
    });
});

function getSpecialConfig() {
    $.ajax({
        type:'get',
        url:BASE_URl + "config/whole",
        success:function (data) {
            if(data.success){
                getSpecialData(data.data);
            }
        }
    });
}

function getSpecialData(configData) {
    var arr = [];
    for(k in configData){
        arr.push(configData[k].k);
    }
    var arrStr = arr.join(",");
    $.ajax({
        type:'get',
        url:BASE_URl + "topic/whole?channel="+getQueryString("channel")+"&key="+arrStr+"&format=price",
        success:function (data) {
            if(data.success){
                DrawSpecial(configData,data.data);
            }
        }
    });
}

function getLimitData() {
    $.ajax({
        type:'get',
        url:BASE_URl + "topic/free?channel=" + getQueryString("channel") +"&format=price&next=false",
        success:function (data) {
            if(data.success){
                countDown(data.data.time[0],data.data.time[1],data.data.time[2]);
                DrawLimit(data.data.list);
            }
        }
    });
}

function DrawSpecial(config,data) {
    var html = "";
    for(var i = 0;i < config.length;i ++){
        var liHtml =  "";
        for(var j = 0;j < data[config[i].k].length;j ++){
            liHtml += `<li class="inline" onclick="goDetail('${data[config[i].k][j].id}')">
                            <div class="book-cover-wrapper"><img src="${data[config[i].k][j].image}" alt="" width="100%" height="100%"></div>
                            <p class="book-name-wrapper">${data[config[i].k][j].name}</p>
                            <div class="book-price-wrapper">
                                <span class="now-price">${data[config[i].k][j].sell_price}书币</span><span class="origin-price">${data[config[i].k][j].original_price}书币</span>
                            </div>
                       </li>`;
        }
        html += `<div class="special-item">
                    <div class="special-item-title">${config[i].v}</div>
                    <div class="special-item-detail">
                        <ul class="tc">` + liHtml + `</ul>
                    </div>
                 </div>`;
    }
    $(".special-content").html(html);
}

function DrawLimit(data) {
    var html = "";
    for(var i = 0;i < data.length;i ++){
        html += `<li class="limit-item inline" onclick="goDetail('${data[i].id}')">
                    <div class="book-cover-wrapper"><img src="${data[i].image}" alt="" width="100%" height="100%"></div>
                    <p class="book-name-wrapper">${data[i].name}</p>
                    <div class="book-price-wrapper"><span class="now-price">免费</span><span class="origin-price">${data[i].original_price}书币</span></div>
                 </li>`;
    }
    $(".limit-list").html(html);
}

function goDetail(id) {
    location.href = "./detail.html?channel="+getQueryString("channel")+"&id="+id;
}

function countDown(hour,min,sec) {
    if (sec < 10) {
        $(".sec").html(0 + '' + sec);
    }else {
        $(".sec").html(sec);
    }
    if (min < 10) {
        $(".min").html(0 + '' + min);
    }else {
        $(".min").html(min);
    }
    if (hour < 10) {
        $(".hour").html(0 + '' + hour);
    }else {
        $(".hour").html(hour);
    }
    var timer = setInterval(function () {
        if (sec < 10) {
            $(".sec").html(0 + '' + sec);
        }else {
            $(".sec").html(sec);
        }
        if (min < 10) {
            $(".min").html(0 + '' + min);
        }else {
            $(".min").html(min);
        }
        if (hour < 10) {
            $(".hour").html(0 + '' + hour);
        }else {
            $(".hour").html(hour);
        }
        sec --;
        if (sec < 0 ){
            sec = 59;
            min --;
            if(min < 0 ){
                min = 59;
                hour --;
                if(hour < 0){
                    hour = 0;
                    min = 0;
                    sec = 0;
                    clearInterval(timer);
                }
            }
        }
    },1000);
}